$(document).ready(function(){
	var beginDate = document.getElementById('beginDate').value;
	var endDate = document.getElementById('endDate').value;
	$.ajax({
		url:"/toGetPayDataForDate",
		type:"post",
		data:{"beginDate":beginDate,"endDate":endDate},
		dataType:"json",
		success:function(result){
			document.getElementById("sum_pay_date").innerHTML = result.sumPayDate;
			document.getElementById("distr_data_date").innerHTML = result.distributeData_date;
		},
		error:function(){
			alert("获取指定期间总消费额发生异常");
		}
	});
	//默认加载最近30天的数据
	getPayData();
});
function getPayData() {
	var beginDate = document.getElementById('beginDate').value;
	var endDate = document.getElementById('endDate').value;
	if(beginDate>endDate){
		alert("开始日期不能大于截止日期，请重新输入");
		return;
	}
	$.ajax({
		url:"/toGetPayDataForDate",
		type:"post",
		data:{"beginDate":beginDate,"endDate":endDate},
		dataType:"json",
		success:function(result){
			document.getElementById("sum_pay_date").innerHTML = result.sumPayDate;
			document.getElementById("distr_data_date").innerHTML = result.distributeData_date;
		},
		error:function(){
			alert("获取指定期间总消费额发生异常");
		}
	});
	//清空内容
	document.getElementById('date_trade').innerHTML="";
	document.getElementById('distribute_d').innerHTML="";
	
	//线性图
	Ext.onReady(function() {
		 //单位数据对象
	Ext.define('PayDataOfWeek',{
		       extend: 'Ext.data.Model',
		       fields: [{name:'payAmountSum',type:'string'},
		                {name:'payDate',type:'string'}
		       ]
		 });
	//分类统计总额数据对象
	Ext.define('PayDataClassify',{
		       extend: 'Ext.data.Model',
		       fields: [{name:'item',type:'string'},
		                {name:'amount',type:'string'}
		       ]
		 });
		 
	var payDataMonthStore = new Ext.data.Store({  
	    pageSize: 20,  
	    model: 'PayDataOfWeek',  
	    autoLoad: true,   
	    proxy: {  
	        type: 'ajax',  
	        url: '/toGetPayDataForDate',  
	        reader: {  
	            type: 'json',  
	            root: 'resultList' 
	        },
	         writer : {
	            type : 'json'
	         },
	         actionMethods : {
	            create : 'POST'
	         }  
	    }  
	     
	});  
	payDataMonthStore.on('beforeload', function (store, options) {  
	     var new_params = { beginDate : document.getElementById('beginDate').value,
		   endDate : document.getElementById('endDate').value };  
	     Ext.apply(store.proxy.extraParams, new_params);
	 });  
	payDataMonthStore.load({  
	    params: { start: 0, limit: 20 }  
	}); 	 
	
	//获取指定期间分类统计数据用于组饼图
	var PayDataClassifyMonthStore = new Ext.data.Store({  
	       pageSize: 20,  
	       model: 'PayDataClassify',  
	       autoLoad: true,   
	       proxy: {  
	           type: 'ajax',  
	           url: '/toGetPayDataForDate',  
	           reader: {  
	               type: 'json',  
	               root: 'pieDataDate' 
	           },
	          writer : {
	             type : 'json'
	          },
	          actionMethods : {
	             create : 'POST'
	          }  
	       }  
	        
	   });  
	PayDataClassifyMonthStore.on('beforeload', function (store, options) {  
	        var new_params = { beginDate : document.getElementById('beginDate').value,
		   endDate : document.getElementById('endDate').value };  
	        Ext.apply(store.proxy.extraParams, new_params);  
	        // alert('beforeload');  
	    });  
	PayDataClassifyMonthStore.load({  
	       params: { start: 0, limit: 20 }  
	   });  

	   Ext.create('Ext.chart.CartesianChart', {
	       renderTo: 'date_trade',
	       width: 746,
	       height: 300,
	       store: payDataMonthStore,
	       legend: {
	           docked: 'bottom'
	       },

	       //define x and y axis.
	       axes: [{
	           type: 'numeric',
	           position: 'left'
	       }, {
	           type: 'category',
	           visibleRange: [0, 1],
	           position: 'bottom'
	       }],

	       //define the actual series
	       series: [{
	           type: 'line',
	           xField: 'payDate',
	           yField: 'payAmountSum',
	           title: '期间消费趋势图'
	       }]
	   });
	 //本月分类统计饼图
	   Ext.create('Ext.chart.PolarChart', {
	       renderTo: 'distribute_d',
	       width: 746,
	       height: 380,
	       store: PayDataClassifyMonthStore,
	       //configure the legend.
	       legend: {
	           docked: 'bottom'
	       },
	       //describe the actual pie series.
	       series: [{
	           type: 'pie',
	           xField: 'amount',
	           label: {
	               field: 'item'
	           },
	           donut: 50 // increase or decrease for increasing or decreasing donut area in middle.
	       }]
	   });
	});
}